package com.delivery.www.servlet;

import com.delivery.www.constant.Constants;
import com.delivery.www.po.Ban;
import com.delivery.www.po.User;
import com.delivery.www.service.BanService;
import com.delivery.www.service.ServiceImpl.BanServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 12970
 */
public class BanServlet extends BaseServlet {
    BanService banService = new BanServiceImpl();

    /**
     * 封禁
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void doBan(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int userId = Integer.parseInt(req.getParameter("userId"));
        String judge = banService.judge(userId);
        if (judge == null) {
            boolean b = banService.doBan(userId);
            if (b) {
                writer.print("ok");
            } else {
                writer.print("error");
            }
        } else {
            writer.print(judge);
        }
    }

    /**
     * 取消封禁
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void cancelBan(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int userId = Integer.parseInt(req.getParameter("userId"));
        boolean b = banService.cancelBan(userId);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 判断是否封禁
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void judgeBan(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        if (user == null) {
            writer.print("login");
        } else {
            Ban ban = banService.get(user.getUserId());
            if (ban == null) {
                writer.print("ok");
            } else {
                writer.print("error");
            }
        }
    }
}
